package com.tvein.web.view
{
	//flash
	import flash.net.LocalConnection;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.events.TimerEvent;
	//pureMVC
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	//custom
	import com.tvein.web.common.CommonConstants;
	import com.tvein.web.model.ServerProxy;
	import com.tvein.web.controller.System.SystemConsole;
	import com.tvein.web.controller.System.SystemGamePlay;
	import com.tvein.web.controller.FileLoader.FileGet;
	import com.tvein.web.controller.FileLoader.preLoad;
//
    public class SystemMediator extends Mediator implements IMediator
    {
        public static const NAME:String = 'SystemMediator';
		
		public function SystemMediator()
		{
			super(NAME);
		}
		
		//Custom
		private var serverData:ServerProxy;
		private var conn:Boolean = false;
		private var webConnection:LocalConnection;
		
		override public function onRegister() : void
        {
			serverData = facade.retrieveProxy( ServerProxy.NAME ) as ServerProxy;
			
			//LSO
			var checkTimer:Timer = new Timer(100);
			checkTimer.addEventListener(TimerEvent.TIMER, openConnection);
			checkTimer.start();
			
			//get server xml
			var loader:URLLoader = new URLLoader();
			loader.load(new URLRequest(CommonConstants.GAME_SERVER + "/server.xml?tmp=" + Math.random()));
			loader.addEventListener(Event.COMPLETE, serverLoad);
			
			//System Command
			/*  -C console  */
			facade.registerCommand(CommonConstants.SYSTEM_CONSOLE, SystemConsole);
			/*  -G gameplay */
			facade.registerCommand(CommonConstants.SYSTEM_GAMEPLAY, SystemGamePlay);
			
			//FileLoader Command
			/*  -F fileget  */
			facade.registerCommand(CommonConstants.FILELOADER_FILEGET, FileGet);
			/*  -P preLoad  */
			facade.registerCommand(CommonConstants.FILELOADER_PRELOAD, preLoad);
        }
		
		private function openConnection(evt:TimerEvent):void
		{
			try
			{
				webConnection = new LocalConnection();
				webConnection.allowDomain(webConnection.domain);
				webConnection.connect(CommonConstants.WEB_CHANNEL);
				
				conn = true;
			}
			catch (error:ArgumentError)
			{
				conn = false;

				webConnection.send(CommonConstants.WEB_CHANNEL, "console", ["close"]);
				webConnection = null;
			}
			
			if (conn)
			{
				webConnection.client = this;
				
				evt.target.stop();
				evt.target.removeEventListener(TimerEvent.TIMER, openConnection);
			}
		}
		
		override public function listNotificationInterests() : Array
		{
			var interests:Array = super.listNotificationInterests();
			interests.push(CommonConstants.SYSTEM);
			interests.push(CommonConstants.FILELOADER);
			return interests;
		}
		
		override public function handleNotification(notification:INotification) : void
		{
			var params:Array = notification.getBody()[0].split(" ");
			
			switch(notification.getName())
			{
				case CommonConstants.SYSTEM:
					switch(params[1])
					{
						case "-C":
							sendNotification(CommonConstants.SYSTEM_CONSOLE, notification.getBody()[1]);
							break;
						case "-G":
							sendNotification(CommonConstants.SYSTEM_GAMEPLAY, notification.getBody()[1]);
							break;
					}
					break;
				case CommonConstants.FILELOADER:
					switch(params[1])
					{
						case "-F":
							sendNotification(CommonConstants.FILELOADER_FILEGET, notification.getBody()[1]);
							break;
						case "-P":
							sendNotification(CommonConstants.FILELOADER_PRELOAD, notification.getBody()[1]);
							break;
						case "-L":
							sendNotification(CommonConstants.FILELOADER_LOADOK, notification.getBody()[1]);
							break;
					}
					break;
			}
		}
		
		public function console(command:Array) : void
		{
			var params:Array = command[0].split(" ");
			
			switch(params[0])
			{
				case "close":
					webConnection.close();
				case "/system":
					sendNotification(CommonConstants.SYSTEM, command);
					break;
				case "/fileloader":
					sendNotification(CommonConstants.FILELOADER, command);
					break;
			}
		}
		
		private function serverLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, serverLoad);
			serverData.serverXML = new XML(evt.target.data);
		}
    }
}